<template>
  <div class="addUser">
    <div class="header">
      <h1>用户管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">用户管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <el-button
          type="info"
          icon="el-icon-arrow-left"
          size="mini"
          @click="goBack"
        >
          返回
        </el-button>
      </div>
      <el-form size="small" class="addForm" ref="addForm" :model="addForm" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="username">
              <span slot="label">
                <span class="labelText">用户名（必填）</span>
              </span>
              <el-input v-model="addForm.username" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="realName">
              <span slot="label">
                <span class="labelText">真实姓名（必填）</span>
              </span>
              <el-input v-model="addForm.realName" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="email">
              <span slot="label">
                <span class="labelText">Email</span>
              </span>
              <el-input v-model="addForm.email" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="degree">
              <span slot="label">
                <span class="labelText">学历</span>
              </span>
              <el-input v-model="addForm.degree" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="idNumber">
              <span slot="label">
                <span class="labelText">身份证号</span>
              </span>
              <el-input v-model="addForm.identityNum" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="departmentId">
              <span slot="label">
                <span class="labelText">部门</span>
              </span>
              <el-select
                v-model="addForm.departmentId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in depArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="occupationId">
              <span slot="label">
                <span class="labelText">职位</span>
              </span>
              <el-select
                v-model="addForm.occupationId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in posArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="salary">
              <span slot="label">
                <span class="labelText">工资</span>
              </span>
              <el-input v-model="addForm.salary" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="birthday">
              <span slot="label">
                <span class="labelText">生日</span>
              </span>
              <el-input placeholder="2023-02-20" v-model="addForm.birthday" :validate-event="false"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="phoneNum">
              <span slot="label">
                <span class="labelText">电话</span>
              </span>
              <el-input v-model="addForm.phoneNum" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="nickname">
              <span slot="label">
                <span class="labelText">昵称</span>
              </span>
              <el-input v-model="addForm.nickname" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="address">
              <span slot="label">
                <span class="labelText">地址</span>
              </span>
              <el-input v-model="addForm.address" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <span slot="label">
                <span class="labelText">密码</span>
              </span>
              <el-input v-model="addForm.password" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="graduateSchool">
              <span slot="label">
                <span class="labelText">毕业院校</span>
              </span>
              <el-input v-model="addForm.graduateSchool" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="bankAccount">
              <span slot="label">
                <span class="labelText">银行账号</span>
              </span>
              <el-input placeholder="工行卡" v-model="addForm.bankAcount" :validate-event="false"></el-input>
            </el-form-item>
            <el-form-item prop="sex">
              <span slot="label" >
                <span class="labelText">性别</span>
              </span>
              <el-select
                v-model="addForm.sex"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in sexArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="roleId">
              <span slot="label">
                <span class="labelText">角色</span>
              </span>
              <el-select
                v-model="addForm.roleId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in occupationArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="joinTime">
              <span slot="label">
                <span class="labelText">入职时间 如2023-02-20 10:26:20</span>
              </span>
              <el-input placeholder="2023-02-20 10:26:20" v-model="addForm.joinTime" :validate-event="false"></el-input>
            </el-form-item>
            <!-- <el-form-item prop="skin">
              <span slot="label">
                <span class="labelText">皮肤</span>
              </span>
              <el-select
                v-model="value"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in skinArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item> -->
          </el-col>
        </el-row>
        <el-form-item class="btnBox">
          <el-button type="primary" size="small" @click="($event) => addUser('addForm')">保存</el-button>
          <el-button type="info" size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  name: "",

  data() {
    return {
      depArr: [],
      posArr:[],
      sexArr:[
        {
          label:"男",
          value:"0"
        },
        {
          label:"女",
          value:"1"
        }
      ],
      occupationArr:[],
      skinArr:[],
      addForm:{
        username:'',
        realName:'',
        email:'',
        degree:'',
        identityNum:'',
        departmentId:'',
        departmentName:'',
        money:'',
        salary:'',
        phoneNum:'',
        nickname:'',
        address:'',
        graduateSchool:'',
        bankAcount:'',
        sex:'',
        occupationId:'',
        occupationName:'',//职位
        joinTime:'',
        colorName:'',
        roleId:'',
        roleName:'',//角色
        salary:'',
        password:''
      },
      rules:{
        username: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        realName: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        sex: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        identityNum: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        roleId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        occupationId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        departmentId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        joinTime: [
          { required: true, message: "不能为空且必须按照规定格式输入", trigger: "blur" },
        ],
      },
      value:"",
    };
  },

  mounted() {},
  created(){
    this.getDepArr()
    this.getPosArr()
    this.getOccupationArr()
  },

  methods: {
    goBack() {
      this.$router.push("/user/userManage");
    },
    // this.$api.user.addUser(this.addForm)
    //增加部门
    addUser(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid){
          /* this.addForm.joinTime = new Date(
          dayjs().format("YYYY-MM-DD HH:mm:ss")
        ).toISOString(); */
        let res = this.$api.user.addUser(this.addForm).then((res) => {
          if (res.data.code === 200) {
            //跳转到列表页面
            this.$router.push("/user/userManage");
            console.log(res);
            //提示
            this.$message({
              message: "添加部门成功",
              type: "success",
            });
          } else {
            //错误信息提示
            this.$message.error("出现错误，添加部门失败");
            console.log(res);

          }
        });
        }else{
          this.$message.error( '请填写必填项')
        }
        
      });
    },
    //取消按钮
    cancel(){
      this.$router.push("/user/userManage")
    },
    //获取部门选项
    async getDepArr(){
      let res = await this.$api.user.getDeptList1()
      let arrData = res.data.data
      arrData.forEach(element => {
        this.depArr.push({label:element.depName,value:element.depId})
      })
    },
    //获取职位选项
    async getPosArr(){
      let res = await this.$api.user.getPosList1()
      let arrData = res.data.data.list
      arrData.forEach(element => {
        this.posArr.push({label:element.title,value:element.id})
      })
    },
    //获取角色选项
    async getOccupationArr(){
      let res = await this.$api.user.getOccupationList1()
      let arrData = res.data.data.list
      arrData.forEach(element => {
        this.occupationArr.push({label:element.roleName,value:element.id})
      })
    },
  },
};
</script>

<style lang="less" scoped>
html body {
  height: 100%;
  background-color: #f5f5f5;
}
.addDep {
  height: 100%;
}
.header {
  position: relative;
  padding: 15px 15px 0 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}
.content {
  padding: 15px;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.labelText {
  color: #333333;
  font-size: 14px;
}
.labelText2 {
  color: #999999;
  font-size: 12px;
  margin-left: 5px;
}
.addForm {
  background-color: #fff;
  padding: 5px;
}
</style>
